package com.cqu.lpss.service.impl;

import com.cqu.lpss.mapper.DangerlogMapper;
import com.cqu.lpss.mapper.ProfessorsMapper;
import com.cqu.lpss.pojo.Dangerlog;
import com.cqu.lpss.pojo.Image;
import com.cqu.lpss.pojo.PageBean;
import com.cqu.lpss.pojo.Professors;
import com.cqu.lpss.service.ProfessorsInterface;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
@Slf4j
@Service
public class ProfessorsImpl implements ProfessorsInterface {
    @Autowired
    ProfessorsMapper professorsMapper;
    @Autowired
    DangerlogMapper dangerlogMapper;
    @Override
    public Professors login(Professors professor) {
        Professors ans = professorsMapper.findByIdAndPassword(professor.getProId(),professor.getProPassword());
        return ans;
    }

    @Override
    public Professors findByIndex(Integer proIndex) {
        Professors ans = professorsMapper.findByIndex(proIndex);
        return ans;
    }

    @Override
    public Professors findById(String proId) {
        Professors ans=professorsMapper.findById(proId);
        return ans;
    }

    @Override
    public PageBean<Professors> list(Integer currentPage, Integer pageSize, String proId, String proName,
                                     Integer proLowAge, Integer proHighAge, Integer proGender, String proEmail,
                                     String proPhone, Integer proAuth, Integer proClass) {
        PageHelper.startPage(currentPage,pageSize);
        List<Professors> profList=professorsMapper.list(proId, proName, proLowAge, proHighAge, proGender,
                proEmail, proPhone, proAuth, proClass);
//        Page<Image> pagelist = (Page<Image>) imageList;
//        PageBean<Image> pageBean = new PageBean<>(pagelist.getTotal(),pagelist);
        Page<Professors> pageList = (Page<Professors>) profList;
        PageBean<Professors> pageBean = new PageBean<>(pageList.getTotal(),pageList);
        return pageBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int danger_delete(String UserId, String UserName, Integer deleteproIndex) {
        Professors deletepro=professorsMapper.findByIndex(deleteproIndex);
        int ans = professorsMapper.delete(deleteproIndex);
        int recordans = danger_record("DELETE", UserId, UserName, deleteproIndex, deletepro.toString(), ans);
        return (ans & recordans);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int danger_add(String UserId,
                          String UserName,
                          Professors addprof) {
        Professors samepro = professorsMapper.findById(addprof.getProId());
        if(samepro!=null){
            return -1;
        }else{

            int ans = professorsMapper.inserte(
                    addprof.getProId(),
                    addprof.getProName(),
                    addprof.getProAge(),
                    addprof.getProGender(),
                    addprof.getProEmail(),
                    addprof.getProPassword(),
                    addprof.getProPhone(),
                    addprof.getProAuth(),
                    addprof.getProClass(),
                    addprof.getProBirth(),
                    new Timestamp(System.currentTimeMillis()),
                    new Timestamp(System.currentTimeMillis()));
            Professors prof = professorsMapper.findById(addprof.getProId());
            Integer proIndex = prof.getProIndex();
            int recordans = danger_record("ADD", UserId, UserName,proIndex,prof.toString(), ans);
            return (ans & recordans);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int danger_edit(String UserId, String UserName, Professors editprof) {
        Professors init_pro = professorsMapper.findByIndex(editprof.getProIndex());
        String edit_des = init_pro.toString()+"Upload IN [ ";
        if(editprof.getProName()!=null){
            edit_des=edit_des+"proName="+editprof.getProName();
        }
        if(editprof.getProEmail()!=null){
            edit_des=edit_des+"  proEmail="+editprof.getProEmail();
        }
        if(editprof.getProPassword()!=null){
            edit_des=edit_des+"  proPassword="+editprof.getProPassword();
        }
        if(editprof.getProPhone()!=null){
            edit_des=edit_des+"  proPhone="+editprof.getProPhone();
        }
        if(editprof.getProClass()!=null){
            edit_des=edit_des+"  proClass="+editprof.getProClass();
        }
        if(editprof.getProAuth()!=null){
            edit_des=edit_des+"  proAuth="+editprof.getProAuth();
        }
        edit_des+="  UpdateTime="+(new Timestamp(System.currentTimeMillis())).toString();
        int ans = professorsMapper.update(editprof.getProIndex()
                ,editprof.getProName(),editprof.getProEmail(),editprof.getProPassword(),editprof.getProPhone(),
                editprof.getProClass(),editprof.getProAuth(),new Timestamp(System.currentTimeMillis()));
        int danger_ans = danger_record("Upload",UserId,UserName,editprof.getProIndex(),edit_des,ans);
        return (ans & danger_ans);

    }

    //    private Integer dangerId;
//    private String  dangerUserId;
//    private String  dangerTable;
//    private Integer dangerTableIndex;
//    private String dangerKind;
//    private String dangerDesc;
//    private java.sql.Timestamp dangerTime;
    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public int danger_record(String DangerKind,String UserId, String UserName, Integer proIndex,
                             String professors,Integer dangerResult) {
        Dangerlog dangerlog = new Dangerlog();
        dangerlog.setDangerUserId(UserId);
        dangerlog.setDangerDesc(DangerKind+":"+professors);
        dangerlog.setDangerKind(DangerKind);
        dangerlog.setDangerTableIndex(proIndex);
        dangerlog.setDangerTable("Professors");
        dangerlog.setDangerTime(new Timestamp(System.currentTimeMillis()));
        dangerlog.setDangerResult(dangerResult);
        log.info("dangerlog:{}",dangerlog);
        int addans = dangerlogMapper.add(dangerlog.getDangerUserId(), dangerlog.getDangerTable(),
                dangerlog.getDangerTableIndex(), dangerlog.getDangerKind(), dangerlog.getDangerDesc(),
                dangerlog.getDangerTime(), dangerlog.getDangerResult());
        return addans;
    }

    @Override
    public Professors findByPhone(String phone) {
        Professors prof = professorsMapper.findByPhone(phone);
        return prof;
    }

    @Override
    public int PersonCenter_upload(String newpassword,String phone,String email, String Id) {
        int ans = professorsMapper.PersonCenter_upload(newpassword, phone, email, Id);
        return ans;

    }
}
